package com.joinway.admin.platform;

import java.util.EnumSet;

import javax.servlet.DispatcherType;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.system.ApplicationPidFileWriter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

@Deprecated
//@SpringBootApplication
@ImportResource("classpath:spring/compact-context.xml")
public class CompactPlatformAdmin {

	@Bean public FilterRegistrationBean filterRegistrationBean () {
		FilterRegistrationBean bean = new FilterRegistrationBean();
		
		bean.setFilter(new CharacterEncodingFilter());
		bean.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
		bean.addUrlPatterns("/*");
		bean.addInitParameter("encoding", "UTF-8");
		bean.addInitParameter("forceEncoding", "true");
		
		return bean;
	}
	
	@Bean public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
	    ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
	    registration.addUrlMappings("*.json", "*.dox");
	    registration.setOrder(2);
	    return registration;
	}
	
	public static void main(String[] args) {
		String pidFile = args != null && args.length > 0 ? args[0] : null;
		
		SpringApplication springApplication = new SpringApplication(CompactPlatformAdmin.class);
//        ApplicationContext ctx = SpringApplication.run(Application.class, args);
		if(pidFile != null){
			springApplication.addListeners(new ApplicationPidFileWriter(pidFile));
		}
		springApplication.run(args);
		
//        System.out.println(System.getProperty("user.home"));
//        System.out.println("Let's inspect the beans provided by Spring Boot:");
    }

}
